#ifndef F_CASgiSPIDriverC_h
#define F_CASgiSPIDriverC_h

// ***********************************************************************

// class EDROOMTopContext

// ***********************************************************************



class EDROOMTopContext {

protected:

enum EDROOMCASgiSPIDriverSignal { EDROOMSignalTimeout, EDROOMSignalDestroy, SNoAnswer, SRxBurst, SRxDataBurst, SAnswer, STxBurst, STxDataBurst };


	friend class CASgiSPIDriver;

	CASgiSPIDriver &EDROOMcomponent;

	CEDROOMMessage * &Msg;

	CEDROOMMessage * &MsgBack;

	//Ports

	CEDROOMInterface & SGIGround;
	CEDROOMTimingInterface & ReTx;

	enum edroomStateId{I, Listening, WaitingGroundResponse};

	enum IdTrans{Init, SendData, SendSignal, NoAnswer, BytesReceived, BytesReceived_MessageNotComplete, BytesReceived_MessageComplete,  EDROOMMEMORIA };

	//Constantes

	const uint16_t VCDataBurstLength;


	//Variables

	CDBurst &VBurst;
	uint16_t &VByteNumber;
	CDDataBurst &VDataBurst;
	CEDROOMTimer &VIdTimer;
	bool_t &VMessageCompleted;
	CEDROOMMessage &VMsgAux;
	byte_t *VStringDataBurst;


	//Pool

	class CEDROOMPOOLCDBurst:public CEDROOMProtectedMemoryPool {
		public:
		CEDROOMPOOLCDBurst(TEDROOMUInt32 elemCount,CDBurst *pMem, TEDROOMBool *pMarks);
		CDBurst	* AllocData();
	};
	CEDROOMPOOLCDBurst	& EDROOMPoolCDBurst;
	class CEDROOMPOOLCDDataBurst:public CEDROOMProtectedMemoryPool {
		public:
		CEDROOMPOOLCDDataBurst(TEDROOMUInt32 elemCount,CDDataBurst *pMem, TEDROOMBool *pMarks);
		CDDataBurst	* AllocData();
	};
	CEDROOMPOOLCDDataBurst	& EDROOMPoolCDDataBurst;


	// CONSTRUCTORES

	EDROOMTopContext (CASgiSPIDriver &act, CDBurst & EDROOMpVarVBurst, uint16_t & EDROOMpVarVByteNumber, CDDataBurst & EDROOMpVarVDataBurst, CEDROOMTimer & EDROOMpVarVIdTimer, bool_t & EDROOMpVarVMessageCompleted, CEDROOMMessage & EDROOMpVarVMsgAux, byte_t * EDROOMpVarVStringDataBurst, CEDROOMPOOLCDBurst & EDROOMpPoolCDBurst, CEDROOMPOOLCDDataBurst & EDROOMpPoolCDDataBurst );

	EDROOMTopContext ( EDROOMTopContext &contex );

public:

	bool EDROOMSearchContextTrans(TEDROOMTransId &edroomCurrentTrans);

	void EDROOMNuevoMens () { EDROOMcomponent.EDROOMNewMsg(); }

	// Funciones Definidas por el usuario

	void	FBuildUpBurst();

	void	FCheckBytesReceived();

	void	FGetData();

	void	FInit();

	void	FListening();

	void	FNoAnswer();

	void	FReTxRandom();

	void	FSendToGround();

};

// ***********************************************************************

// class EDROOMTopState

// ***********************************************************************



class EDROOMTopState : public EDROOMTopContext {

protected:



	EDROOMTopContext::edroomStateId edroomCurrentState;

	EDROOMTopContext::edroomStateId edroomNextState;

	//Variables

	CDBurst VBurst;
	uint16_t VByteNumber;
	CDDataBurst VDataBurst;
	CEDROOMTimer VIdTimer;
	bool_t VMessageCompleted;
	CEDROOMMessage VMsgAux;
	byte_t VStringDataBurst[8224];


	//Pools

	CEDROOMPOOLCDBurst	EDROOMPoolCDBurst;
	CEDROOMPOOLCDDataBurst	EDROOMPoolCDDataBurst;


public:

	// CONSTRUCTOR

	EDROOMTopState (CASgiSPIDriver &act, CEDROOMMemory *pEDROOMMemory  );


	// EDROOMBehaviour**********************************************

	void EDROOMBehaviour();

	// Inicio**********************************************

	void EDROOMInicio();

	// ***********************************************************************

	// SubEstado Inicial 

	// ***********************************************************************



	TEDROOMTransId EDROOMIllegada();

	// ***********************************************************************

	// SubEstado Hoja Listening

	// ***********************************************************************



	TEDROOMTransId EDROOMListeningllegada();

	// ***********************************************************************

	// SubEstado Hoja WaitingGroundResponse

	// ***********************************************************************



	TEDROOMTransId EDROOMWaitingGroundResponsellegada();

};


#endif
